package InventoryAssetList

import (
	"ToolLibray/jwtx"
	"context"
	"property_api/internal/svc"
	"property_api/internal/types"
	"property_api/utils/result"
	"property_api/utils/xform"
	"property_service/client/inventory_service"

	"github.com/zeromicro/go-zero/core/logx"
)

type EditLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EditLogic {
	return &EditLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// Edit 修改资产入库清单的逻辑
func (l *EditLogic) Edit(req *types.InventoryAssetListEdit) (resp *types.Response, err error) {
	// 设置租户ID，从上下文中获取
	tenantID := jwtx.GetUserTenantId64(l.ctx)
	// 设置更新用户，从上下文中获取
	userIdStr := xform.Int64ToString(jwtx.GetUserId(l.ctx))

	// 调用服务层方法，修改资产入库清单记录
	// 服务层方法 InventoryAssetListEdit 会处理实际的数据库更新操作
	res, err := l.svcCtx.InventoryService.InventoryAssetListEdit(l.ctx, &inventory_service.InventoryAssetList{
		ID:                req.ID,                // 记录ID
		TenantID:          tenantID,              // 租户ID
		AssetID:           req.AssetID,           // 资产ID（入库成功关联资产表）
		ReceiptID:         req.ReceiptID,         // 入库单ID（外键关联资产入库表）
		AssetName:         req.AssetName,         // 资产名称
		AssetCategory:     req.AssetCategory,     // 资产类别
		AssetCategoryId:   req.AssetCategoryId,   //资产类别ID
		Unit:              req.Unit,              //单位
		AssetCode:         req.AssetCode,         // 资产编号
		SpecModel:         req.SpecModel,         // 规格型号
		UnitPrice:         req.UnitPrice,         // 单价
		Quantity:          req.Quantity,          // 数量
		OriginalValue:     req.OriginalValue,     // 资产原值
		InstallationCost:  req.InstallationCost,  // 安装费用
		MiscellaneousFees: req.MiscellaneousFees, // 杂运费
		Tax:               req.Tax,               // 税费
		FundingSource:     req.FundingSource,     // 资金来源
		IsAccessory:       req.IsAccessory,       // 是否附属设备
		Manufacturer:      req.Manufacturer,      // 生产厂商
		AcceptanceDate:    req.AcceptanceDate,    // 验收日期
		AllocationInfo:    req.AllocationInfo,    // 领用信息
		Remarks:           req.Remarks,           // 备注
		PurchaseID:        req.PurchaseID,        // 采购申请单ID
		PurchaseAssetID:   req.PurchaseAssetID,   // 采购资产详细单ID
		PurchaseCode:      req.PurchaseCode,      // 采购单编码
		Attachment:        req.Attachment,        // 附件
		StatusID:          req.StatusID,          // 状态ID(关联状态表)
		UpdatedAtuser:     userIdStr,             // 更新用户
	})

	// 如果服务层返回错误，返回统一的错误响应
	if err != nil {
		return result.Err(err).End()
	}

	// 返回成功的响应
	return result.Suc(res).End()
}
